﻿using HKB.Common;
using HKB.Web;
using HKB.Web.Admin;
using System;
using System.Data;
using System.IO;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI.WebControls;
using System.Xml;

public partial class Admin_Admin_ManageTemProject : Admin, IRequiresSessionState
{
    private TemProjectBll _temproject = null;
    protected DefaultProfile profile
    {
        get
        {
            return (DefaultProfile)this.Context.Profile;
        }
    }
    protected global_asax applicationInstance
    {
        get
        {
            return (global_asax)this.Context.ApplicationInstance;
        }
    }
    protected TemProjectBll m_TemProject
    {
        get
        {
            TemProjectBll temproject;
            if (this._temproject == null)
            {
                this._temproject = new TemProjectBll(this.conn);
                temproject = this._temproject;
            }
            else
            {
                temproject = this._temproject;
            }
            return temproject;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!base.admin.CheckAdminPower(44))
        {
            base.WriteLog("View", Language.GetAdminMsg("Log/Access", "0"), "Template");
            base.WriteRightErrMsg();
        }
        if (!base.IsPostBack)
        {
            this.GridView1.DataSource = this.m_TemProject.GetProjects();
            this.GridView1.DataBind();
        }
    }
    protected void SetDefault_OnClick(object sender, CommandEventArgs e)
    {
        if (!base.admin.CheckAdminPower(45))
        {
            base.WriteRightErrMsg();
        }
        if (e.CommandArgument != null)
        {
            int num = Convert.ToInt32(e.CommandArgument.ToString());
            DataTable projectByID = this.m_TemProject.GetProjectByID(num);
            string text = projectByID.Rows[0]["Content"].ToString();
            if (string.IsNullOrEmpty(text))
            {
                this.WriteErrMsg("", Language.GetAdminMsg("TemplateProject/SelectErr"), "");
            }
            this.SetConfig(text);
            this.m_TemProject.SetDefault(num);
            string isDefaultInProject = projectByID.Rows[0]["TemplateProjectName"].ToString();
            SkinBll skinBll = new SkinBll(this.conn);
            skinBll.SetIsDefaultInProject(isDefaultInProject);
            this.GridView1.DataSource = this.m_TemProject.GetProjects();
            this.GridView1.DataBind();
        }
    }
    private bool SetConfig(string m_Content)
    {
        XmlHelper xmlHelper = new XmlHelper();
        xmlHelper.CreateXml("1.0", "GB2312", null, "Root");
        xmlHelper.SetRootXml(m_Content);
        DataSet dataSet = xmlHelper.GetDataSet();
        DataTable dataTable = dataSet.Tables["TemplateSetList"];
        DataRow dataRow = dataTable.Rows[0];
        string nodeText = xmlHelper.GetNodeText("Root/TemplateDir");
        string userReg = (string)dataRow["UserReg"];
        string login = (string)dataRow["Login"];
        string passport = (string)dataRow["Password"];
        string text = (string)dataRow["Passport"];
        string showProduct = (string)dataRow["ShowProduct"];
        string sell = (string)dataRow["Sell"];
        string productCompare = (string)dataRow["ProductCompare"];
        string browseoffer = (string)dataRow["Browseoffer"];
        string publishPreview = (string)dataRow["PublishPreview"];
        string editProduct = (string)dataRow["EditProduct"];
        string saleProduct = (string)dataRow["SaleProduct"];
        string classChoose = (string)dataRow["ClassChoose"];
        string requestProduct = (string)dataRow["RequestProduct"];
        string categoryList = (string)dataRow["CategoryList"];
        string productList = (string)dataRow["ProductList"];
        string shopList = (string)dataRow["ShopList"];
        string shopIndex = (string)dataRow["ShopIndex"];
        string shopBuy = (string)dataRow["ShopBuy"];
        string index = (string)dataRow["Index"];
        string specialDefault = (string)dataRow["SpecialDefault"];
        string searchProduct = (string)dataRow["SearchProduct"];
        string searchShop = (string)dataRow["SearchShop"];
        string article = (string)dataRow["Article"];
        string articleList = (string)dataRow["ArticleList"];
        string announce = (string)dataRow["Announce"];
        string shopReview = (string)dataRow["ShopReview"];
        string shopIntro = (string)dataRow["ShopIntro"];
        string seniorShopIndex = (string)dataRow["SeniorShopIndex"];
        string commonShopIndex = (string)dataRow["CommonShopIndex"];
        string shopAllMessage = (string)dataRow["ShopAllMessage"];
        string memberIndex = (string)dataRow["MemberIndex"];
        string identifySuccess = (string)dataRow["IdentifySuccess"];
        string identifyCompany = (string)dataRow["IdentifyCompany"];
        string identifyUser = (string)dataRow["IdentifyUser"];
        string identifyAgree = (string)dataRow["IdentifyAgree"];
        string identifyChoose = (string)dataRow["IdentifyChoose"];
        string identifyAlert = (string)dataRow["IdentifyAlert"];
        string shopingCar = (string)dataRow["ShopingCar"];
        string adminError = (string)dataRow["AdminError"];
        string adminSuccess = (string)dataRow["AdminSuccess"];
        string shopError = (string)dataRow["ShopError"];
        string shopSuccess = (string)dataRow["ShopSuccess"];
        string memberError = (string)dataRow["MemberError"];
        string memberSuccess = (string)dataRow["MemberSuccess"];
        SiteConfigBll siteConfigBll = new SiteConfigBll();
        siteConfigBll.UpdateTemplateSet(userReg, login, passport, showProduct, sell, productCompare, categoryList, productList, shopList, shopBuy, shopIndex, index, specialDefault, browseoffer, searchProduct, article, articleList, shopReview, shopIntro, memberIndex, identifySuccess, identifyCompany, identifyUser, identifyAgree, identifyChoose, publishPreview, announce, commonShopIndex, seniorShopIndex, classChoose, saleProduct, editProduct, identifyAlert, searchShop, shopAllMessage, requestProduct, shopingCar, adminError, adminSuccess, shopError, shopSuccess, memberError, memberSuccess);
        siteConfigBll.UpDateConfigNode("SiteConfigInfo/TemplateDir", nodeText);
        return true;
    }
    protected void DeleteProject_OnClick(object sender, CommandEventArgs e)
    {
        if (!base.admin.CheckAdminPower(46))
        {
            base.WriteRightErrMsg();
        }
        if (e.CommandArgument != null)
        {
            int iD = Convert.ToInt32(e.CommandArgument.ToString());
            string projectNameByID = this.m_TemProject.GetProjectNameByID(iD);
            this.m_TemProject.DeleteProject(iD);
            SkinBll skinBll = new SkinBll(this.conn);
            skinBll.DeleteSkinByProName(projectNameByID);
            this.GridView1.DataSource = this.m_TemProject.GetProjects();
            this.GridView1.DataBind();
        }
    }
    protected void OutPutTem(object sender, CommandEventArgs e)
    {
        string text = "";
        int num = 0;
        XmlHelper xmlHelper = new XmlHelper();
        xmlHelper.Load(base.Server.MapPath("~/") + "\\App_Data\\SiteConfig.config");
        string text2 = xmlHelper.GetNodeText("SiteConfigInfo/TemplateDir");
        text2 = text2.Replace("/", "\\");
        XmlNodeList nodesList = xmlHelper.GetNodesList("SiteConfigInfo/TemplateSetList");
        xmlHelper.SetRootXml(nodesList[0].InnerXml);
        num += xmlHelper.GetXml().DocumentElement.ChildNodes.Count;
        SkinBll skinBll = new SkinBll(this.conn);
        string defaultProjectName = this.m_TemProject.GetDefaultProjectName();
        string skinDir = this.m_TemProject.GetSkinDir();
        DataTable skinByProjectName = skinBll.GetSkinByProjectName(defaultProjectName);
        num += skinByProjectName.Rows.Count;
        string[] array = new string[num];
        if (skinByProjectName.Rows.Count > 0)
        {
            text = "<Skin>";
            for (int i = 0; i < skinByProjectName.Rows.Count; i++)
            {
                array[i] = base.Server.MapPath("~/") + "Skin/" + skinDir + skinByProjectName.Rows[i]["SkinPath"].ToString().Replace("/", "\\");
                object obj = text;
                text = string.Concat(new object[]
				{
					obj,
					"<Item><SkinName>",
					skinByProjectName.Rows[i]["SkinName"],
					"</SkinName><SkinPath>",
					skinByProjectName.Rows[i]["SkinPath"],
					"</SkinPath><SkinType>",
					skinByProjectName.Rows[i]["SkinType"],
					"</SkinType><SkinPreviewImg>",
					skinByProjectName.Rows[i]["SkinPreviewImg"],
					"</SkinPreviewImg><IsDefault>",
					(skinByProjectName.Rows[i]["IsDefault"].ToString() == "True") ? "1" : "0",
					"</IsDefault></Item>"
				});
            }
            text += "</Skin>";
        }
        for (int i = 0; i < xmlHelper.GetXml().DocumentElement.ChildNodes.Count; i++)
        {
            array[skinByProjectName.Rows.Count + i] = Fetch.GetTruePath(text2 + xmlHelper.GetXml().DocumentElement.ChildNodes[i].InnerText);
        }
        DataTable projectByName = this.m_TemProject.GetProjectByName(defaultProjectName);
        text = string.Concat(new object[]
		{
			"<?xml version=\"1.0\" encoding=\"utf-8\"?><root><Intro>",
			projectByName.Rows[0]["Intro"],
			"</Intro><SkinDir>",
			projectByName.Rows[0]["SkinDir"],
			"</SkinDir><TemplateSetList>",
			nodesList[0].InnerXml,
			"</TemplateSetList>",
			text,
			"</root>"
		});
        string text3 = defaultProjectName + ".rar";
        ZipFile zipFile = new ZipFile();
        zipFile.Create(array, base.Server.MapPath("~/"), base.Server.MapPath("~/") + "\\Template\\" + text3, 1, text, string.Concat(new string[]
		{
			"Template\\",
			defaultProjectName,
			"\\",
			defaultProjectName,
			".config"
		}));
        base.Response.Clear();
        FileInfo fileInfo = new FileInfo(base.Server.MapPath("~/") + "\\Template\\" + text3);
        base.Response.AddHeader("content-type", "application/zip");
        base.Response.Charset = "utf-8";
        base.Response.AddHeader("content-length", fileInfo.Length.ToString());
        base.Response.AddHeader("content-disposition", "attachment;filename=" + text3);
        base.Response.WriteFile(base.Server.MapPath("~/") + "\\Template\\" + text3);
    }
    protected void ShowNewProject(object sender, CommandEventArgs e)
    {
        int iD = Convert.ToInt32(e.CommandArgument.ToString());
        this.NewPanel.Visible = true;
        this.TemplateProjectID.Value = iD.ToString();
        TemProjectBll temProjectBll = new TemProjectBll(this.conn);
        this.OldTemplateProjectName.Text = temProjectBll.GetProjectNameByID(iD);
    }
    protected void NewProject(object sender, CommandEventArgs e)
    {
        int num = Convert.ToInt32(this.TemplateProjectID.Value);
        if (num > 0)
        {
            TemProjectBll temProjectBll = new TemProjectBll(this.conn);
            DataTable projectByName = temProjectBll.GetProjectByName(this.TemplateProjectName.Text);
            if (projectByName.Rows.Count > 0)
            {
                this.WriteErrMsg("", "方案已存在！", "");
            }
            if (this.SkinDir.Text == "")
            {
                this.WriteErrMsg("", "风格目录不能为空！", "");
            }
            string text = base.Server.MapPath("~/") + "Skin\\" + this.SkinDir.Text;
            FileIO fileIO = new FileIO();
            if (Directory.Exists(text))
            {
                this.WriteErrMsg("", "风格目录已经存在！", "");
            }
            string text2 = base.Server.MapPath("~/") + "Template\\" + this.TemplateProjectName.Text;
            try
            {
                fileIO.CreateFolder(text);
                fileIO.CreateFolder(text2);
            }
            catch
            {
                fileIO.DeleteFileFolder(text);
                fileIO.DeleteFileFolder(text2);
                this.WriteErrMsg("", "复制文件失败，请检查路径是否有写入权限！", "");
            }
            finally
            {
                fileIO.DeleteFileFolder(text);
                fileIO.DeleteFileFolder(text2);
            }
            string str = (string)this.conn.GetScalar("Select SkinDir From [HL_TemplateProject] Where TemplateProjectID=" + num, new IDbDataParameter[0]);
            fileIO.CopyDirectoryFile(base.Server.MapPath("~/") + "Skin\\" + str, text);
            fileIO.CopyDirectoryFile(base.Server.MapPath("~/") + "Template\\" + this.OldTemplateProjectName.Text, text2);
            if (File.Exists(text2 + "\\" + this.OldTemplateProjectName.Text + ".config"))
            {
                fileIO.ReName(text2 + "\\" + this.TemplateProjectName.Text + ".config", text2 + "\\" + this.OldTemplateProjectName.Text + ".config");
            }
            if (temProjectBll.BakProject(this.TemplateProjectName.Text, num, this.SkinDir.Text))
            {
                SkinBll skinBll = new SkinBll(this.conn);
                skinBll.BakProjectSkin(temProjectBll.GetProjectNameByID(num), this.TemplateProjectName.Text);
            }
        }
        this.Finish();
        base.Response.Redirect("Admin_ManageTemProject.aspx", true);
    }
    protected void GridView1_DataBound(object sender, EventArgs e)
    {
        foreach (GridViewRow gridViewRow in this.GridView1.Rows)
        {
            if (gridViewRow.RowType == DataControlRowType.DataRow)
            {
                gridViewRow.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#E7EDFE'");
                gridViewRow.Attributes.Add("onmouseout", "this.style.backgroundColor=c;");
                if (gridViewRow.Cells[2].Text.ToLower() == "true")
                {
                    gridViewRow.Cells[2].Text = Language.Get("Common/Yes");
                    LinkButton linkButton = (LinkButton)gridViewRow.FindControl("SetDefaultButton");
                    LinkButton linkButton2 = (LinkButton)gridViewRow.FindControl("UnInstallProButton");
                    linkButton.OnClientClick = "";
                    linkButton.Enabled = false;
                    linkButton2.OnClientClick = "";
                    linkButton2.Enabled = false;
                }
                else
                {
                    gridViewRow.Cells[2].Text = Language.Get("Common/No");
                }
            }
        }
    }
}
